﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KNS.Invoice.UI;
using System.Globalization;
using Resource_Objects;
using System.IO;
using System.Diagnostics;

namespace ResourceManagement
{
    public partial class MainForm : KNSFormBase
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void barMainMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string key;

                //if (e.Item.Name.EndsWith("ToolStripMenuItem"))
                //{
                //    key = e.Item.Name.Substring(0, e.Item.Name.Length - 17);
                //}
                if (e.Item.Name.EndsWith("ToolStripButton"))
                {
                    key = e.Item.Name.Substring(0, e.Item.Name.Length - 15);
                }
                else
                {
                    key = e.Item.Name;
                }

                if (key.ToLower() == "hidetranslated")
                {
                    // this is handled elsewhere (search for 'hidetranslated')
                    return;
                }

                if (key.ToLower() == "hidelocked")
                {
                    // this is handled elsewhere (search for 'hidelocked')
                    return;
                }

                this.ExecCommand(key);
            }
            catch (Exception ex)
            {
                ShowExceptionMessageBox(ex);
            }
        }

        private void ExecCommand(string key)
        {
            key = key.ToLower();
            
            switch (key)
            {
                case "open":
                    this.ResourceBundle_Open();
                    break;
                case "save":
                    this.ResourceBundle_Save();
                    break;
                case "addculture":
                    this.ResourceSet_Add();
                    break;
                case "removeculture":
                    this.ResourceSet_Delete();
                    break;
                case "increasefont":
                    //this.CustomDataGrid1.SetGridFont(CustomDataGrid1.MainDataGridView.Font.Size + 2);
                    break;
                case "decreasefont":
                    //this.CustomDataGrid1.SetGridFont(CustomDataGrid1.MainDataGridView.Font.Size - 2);
                    break;
                case "showworkingfolder":
                    try
                    {
                        //FileInfo baseFileInfo = new FileInfo(this.ContextMenu.CurrentBaseFile);
                        //using (Process p = new Process())
                        //{
                        //    p.StartInfo = new ProcessStartInfo("explorer", baseFileInfo.DirectoryName);
                        //    p.Start();
                        //}
                    }
                    catch
                    {
                        // there is some very strange case where Process.Start throws 'File not found' exception altough process does start.
                        // I hate catching exceptions without doing something with the exception, but in this case it seems there is nothing else to do!
                    }

                    break;
                case "hidetranslated":
                    this.ApplyCriteria();
                    break;
                case "copybasevalue":
                    this.CopyBaseValue();
                    break;
                case "hidelocked":
                    this.ApplyCriteria();
                    break;
                case "hidevsformitems":
                    this.ApplyCriteria();
                    break;
                case "resexhome":
                    //Main.OpenURL(Main.ResExHomePage);
                    break;
                case "discuss":
                    //Main.OpenURL(Main.ForumsURL);
                    break;
                case "checkforupdates":
                    //Main.CheckForUpdates(false);
                    break;
                case "helpcontents":
                    //Help.ShowHelp(this, this.HelpNamespace);
                    break;
                case "about":
                    //AboutBox about = new AboutBox();
                    //about.ShowDialog();
                    break;
                case "exit":
                    Close();
                    break;
            }
        }

        private void CopyBaseValue()
        {
            // if a cell is selected
            //if (CustomDataGrid1.CurrentCell != null)
            //{
            //    DataGridViewRow row = CustomDataGrid1.MainDataGridView.Rows[CustomDataGrid1.CurrentCell.RowIndex];
            //    ResourceItem baseResourceItem = (ResourceItem)row.Tag;

            //    // if value is allowed to be localized
            //    if (!baseResourceItem.Locked)
            //    {
            //        // if current column is not LocalValue, make it current
            //        if (CustomDataGrid1.CurrentColumn.Name != ColumnNames.LocalValue)
            //        {
            //            CustomDataGrid1.CurrentCell = row.Cells[ColumnNames.LocalValue];
            //        }

            //        // if not currently in edit mode, then begin edit mode
            //        if (!CustomDataGrid1.MainDataGridView.IsCurrentCellInEditMode)
            //        {
            //            CustomDataGrid1.MainDataGridView.BeginEdit(false);
            //        }

            //        // get instance of editor of cell and set base value
            //        DataGridViewTextBoxEditingControl editControl = (DataGridViewTextBoxEditingControl)CustomDataGrid1.MainDataGridView.EditingControl;
            //        editControl.Text = row.Cells[ColumnNames.BaseValue].Value.ToString();
            //        editControl.SelectAll();
            //    }
            //}
        }

        #region " Add/Remove/Select Culture "

        private void ResourceSet_Delete()
        {
            //if (CultureToolStripComboBox.SelectedIndex != -1)
            //{
            //    // prevent deletion of the last resource set
            //    if (this.Context.CurrentResourceBundle.Where(p => p.Status != ResourceSetStatus.Deleted).Count() == 1)
            //    {
            //        MessageBox.Show("Deletion of the last resource set of the bundle is not possible!", Tools.GetAssemblyProduct(), MessageBoxButtons.OK, MessageBoxIcon.Stop);
            //        return;
            //    }

            //    string message = String.Format(My.Resources.Local.DeletingResourceSetWarning, this.Context.CurrentLocalResourceSet.Culture);
            //    if (MessageBox.Show(message, Tools.GetAssemblyProduct(), MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
            //    {
            //        // mark as deleted in collection
            //        this.Context.CurrentResourceBundle.CultureRemove(CultureToolStripComboBox.Text);

            //        // remove from UI
            //        CultureToolStripComboBox.Items.RemoveAt(CultureToolStripComboBox.SelectedIndex);

            //        // set current resource to nothing
            //        this.Context.CurrentLocalResourceSet = null;

            //        // select another culture as current
            //        if (CultureToolStripComboBox.Items.Count > 0)
            //        {
            //            CultureToolStripComboBox.SelectedIndex = 0;
            //        }

            //        this.RefreshButtonState();
            //    }
            //}
        }

        private void ResourceSet_Add()
        {
            //using (CultureSelectForm cultureSelectForm = new CultureSelectForm())
            //{
            //    if (cultureSelectForm.ShowDialog() == DialogResult.OK)
            //    {
            //        this.ResourceSet_Add(cultureSelectForm.SelectedCulture);
            //    }
            //}
        }

        private void ResourceSet_Add(CultureInfo culture)
        {
            //if (this.Context.CurrentResourceBundle.ContainsCulture(culture.Name))
            //{
            //    MessageBox.Show(My.Resources.Local.CultureAlreadyExistsError, Tools.GetAssemblyProduct(), MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            //else
            //{
            //    // add to collection
            //    this.Context.CurrentResourceBundle.Add(culture.Name);

            //    // add to UI
            //    CultureToolStripComboBox.Items.Add(culture);

            //    // select it
            //    CultureToolStripComboBox.SelectedIndex = CultureToolStripComboBox.Items.Count - 1;

            //    this.RefreshButtonState();
            //}
        }

        private void CultureToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.ResourceSet_SetCurrent(CultureToolStripComboBox.Text);
        }

        /// <summary>
        /// Sets the current resource file.
        /// </summary>
        private void ResourceSet_SetCurrent(string culture)
        {
            //this.Context.CurrentLocalResourceSet = null;
            //try
            //{
            //    // unsubscribe from the events of the previous resource set
            //    if (this.Context.CurrentLocalResourceSet != null)
            //    {
            //        this.Context.CurrentLocalResourceSet.CollectionChanged -= this.CurrentLocalResourceSet_CollectionChanged;
            //        this.Context.CurrentLocalResourceSet.ResourceItemChanged -= this.CurrentLocalResourceSet_ResourceItemChanged;
            //    }

            //    ResourceSet localResourceSetTemp = this.Context.CurrentResourceBundle[culture];

            //    this.gridLoading = true;
            //    try
            //    {
            //        foreach (DataGridViewRow row in CustomDataGrid1.MainDataGridView.Rows)
            //        {
            //            this.UpdateGridRowFromResourceItem(row, localResourceSetTemp);
            //        }
            //    }
            //    finally
            //    {
            //        this.gridLoading = false;
            //    }

            //    this.ProgressSetMax(this.Context.CurrentBaseResourceSet.CountStringItems(false) - this.Context.CurrentBaseResourceSet.CountLocked());
            //    this.ProgressValueSet(localResourceSetTemp.CountTranslatedItems(this.Context.CurrentBaseResourceSet), localResourceSetTemp.CountMarkedForReviewing());

            //    this.Context.CurrentLocalResourceSet = localResourceSetTemp;

            //    this.SelectFirstVisibleRow();

            //    // subscribe to events of the resource set
            //    this.Context.CurrentLocalResourceSet.CollectionChanged += this.CurrentLocalResourceSet_CollectionChanged;
            //    this.Context.CurrentLocalResourceSet.ResourceItemChanged += this.CurrentLocalResourceSet_ResourceItemChanged;
            //}
            //catch
            //{
            //    // If exception occurs (grid is left partially updated), the following makes sure that the end-user 
            //    // will not be able to save it and that buttons are refreshed as if no resource set was active.
            //    this.Context.CurrentProjectIsDirty = false;
            //    this.RefreshButtonState();

            //    throw;
            //}
        }

        #endregion

        #region " Binding - We don't use real windows forms binding so we need to transfer changes from data to UI and back programmatically "

        // this is triggered when a new item is added to the base local resource set
        //private void CurrentBaseResourceSet_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
        //    var resourceSet = (ResourceSet)sender;
        //    this.Context.CurrentProjectIsDirty = true;

        //    if (resourceSet.Status == ResourceSetStatus.Unaffected)
        //    {
        //        resourceSet.Status = ResourceSetStatus.Updated;
        //    }

        //    this.RefreshButtonState();
        //}

        // this is triggered when a property of an item that belongs to the base local resource set changes
        //private void CurrentBaseResourceSet_ResourceItemChanged(object sender, ResourceItemChangedEventArgs e)
        //{
        //    var resourceSet = (ResourceSet)sender;
        //    this.Context.CurrentProjectIsDirty = true;

        //    if (resourceSet.Status == ResourceSetStatus.Unaffected)
        //    {
        //        resourceSet.Status = ResourceSetStatus.Updated;
        //    }

        //    this.RefreshButtonState();
        //}

        // this is triggered when a new item is added to the current local resource set
        //private void CurrentLocalResourceSet_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
        //    ResourceSet resourceSet = (ResourceSet)sender;

        //    foreach (ResourceItem newItem in e.NewItems)
        //    {
        //        foreach (DataGridViewRow row in CustomDataGrid1.MainDataGridView.Rows)
        //        {
        //            string rowKey = row.Cells[ColumnNames.Key].Value.ToString();

        //            if (rowKey == newItem.Name)
        //            {
        //                this.UpdateGridRowFromResourceItem(row, resourceSet);
        //            }
        //        }
        //    }

        //    this.ProgressValueSet(resourceSet.CountTranslatedItems(this.Context.CurrentBaseResourceSet), resourceSet.CountMarkedForReviewing());
        //}

        // this is triggered when a property of an item that belongs to the current local resource set changes
        //private void CurrentLocalResourceSet_ResourceItemChanged(object sender, ResourceItemChangedEventArgs e)
        //{
        //    ResourceSet resourceSet = (ResourceSet)sender;

        //    foreach (DataGridViewRow row in CustomDataGrid1.MainDataGridView.Rows)
        //    {
        //        string rowKey = row.Cells[ColumnNames.Key].Value.ToString();

        //        if (rowKey == e.Item.Name)
        //        {
        //            this.UpdateGridRowFromResourceItem(row, resourceSet);
        //        }
        //    }

        //    this.ProgressValueSet(resourceSet.CountTranslatedItems(this.Context.CurrentBaseResourceSet), resourceSet.CountMarkedForReviewing());
        //}

        /// <summary>
        /// Updates the given grid row using the corresponding resource item of the given resource set
        /// </summary>
        private void UpdateGridRowFromResourceItem(DataGridViewRow row, ResourceSet resourceSet)
        {
            //string rowKey = row.Cells[ColumnNames.Key].Value.ToString();

            //// if given resource set does not contain resource item for the row
            //// then put all values to null
            //if (!resourceSet.ContainsKey(rowKey))
            //{
            //    row.Cells[ColumnNames.LocalValue].Value = null;
            //    row.Cells[ColumnNames.LocalComment].Value = null;
            //    RowSetStatus(row, null);
            //}
            //else
            //{
            //    ResourceItem resourceItem = resourceSet[rowKey];
            //    row.Cells[ColumnNames.LocalValue].Value = resourceItem.Value;
            //    row.Cells[ColumnNames.LocalComment].Value = resourceItem.Comment;
            //    RowSetStatus(row, resourceItem);
            //}

            //row.Height = row.GetPreferredHeight(row.Index, DataGridViewAutoSizeRowMode.AllCells, true);
        }

        /// <summary>
        /// Updates the given grid row using the corresponding resource item of the given resource set
        /// </summary>
        private void UpdateResourceItemFromGridRow(DataGridViewRow row)
        {
            //ResourceSet resourceSet = this.Context.CurrentLocalResourceSet;
            //string rowKey = row.Cells[ColumnNames.Key].Value.ToString();

            //ResourceItem localResourceItem;

            //// add resource item or get existing
            //bool isNewItem;
            //if (!resourceSet.ContainsKey(rowKey))
            //{
            //    localResourceItem = new ResourceItem();
            //    localResourceItem.Name = rowKey;
            //    isNewItem = true;
            //}
            //else
            //{
            //    localResourceItem = resourceSet[rowKey];
            //    isNewItem = false;
            //}

            //// save value of row
            //localResourceItem.Value = row.Cells[ColumnNames.LocalValue].Value != null ? row.Cells[ColumnNames.LocalValue].Value.ToString() : null;

            //// save comment of row
            //localResourceItem.Comment = row.Cells[ColumnNames.LocalComment].Value != null ? row.Cells[ColumnNames.LocalComment].Value.ToString() : null;

            //if (isNewItem)
            //{
            //    resourceSet.Add(rowKey, localResourceItem);
            //}

            //if (resourceSet.Status == ResourceSetStatus.Unaffected)
            //{
            //    resourceSet.Status = ResourceSetStatus.Updated;
            //}

            //this.RefreshButtonState();
        }

        #endregion

        #region " Open/Save Code "

        private void ResourceBundle_Open()
        {
            this.ResourceBundle_Open(null);
        }

        private void ResourceBundle_Open(string fileName)
        {
            if (this.AskToSave())
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        fileName = openFileDialog1.FileName;
                    }
                    else
                    {
                        return;
                    }
                }

                // change current directory to match the files directory
                FileInfo fileInfo = new FileInfo(fileName);

                // unsubscribe to change events of previous base resource set
                //if (this.Context.CurrentBaseResourceSet != null)
                //{
                //    this.Context.CurrentBaseResourceSet.CollectionChanged -= this.CurrentBaseResourceSet_CollectionChanged;
                //    this.Context.CurrentBaseResourceSet.ResourceItemChanged -= this.CurrentBaseResourceSet_ResourceItemChanged;
                //}

                //ResXResourceBundleAdapter resXResourceBundle = new ResXResourceBundleAdapter(new StandardAdapters.Common.FileSystem());
                //this.Context.CurrentResourceBundle = resXResourceBundle.Load(fileName);
                //this.Context.CurrentBaseFile = fileName;
                //this.Context.CurrentBaseResourceSet = this.Context.CurrentResourceBundle.NeutralOrFirst();

                //// subscribe to change events of base resource set
                //this.Context.CurrentBaseResourceSet.CollectionChanged += this.CurrentBaseResourceSet_CollectionChanged;
                //this.Context.CurrentBaseResourceSet.ResourceItemChanged += this.CurrentBaseResourceSet_ResourceItemChanged;

                // load bundle on UI
                this.RefreshCurrentBundle();

                // insert into recent items list
                //string fileNameStipped = StringTools.CompactText(fileName, 50);
                //RecentItemsManager.PushItem(new RecentItem(fileNameStipped, "file", fileName));

                //// show caption
                //Text = fileInfo.Name + " - " + Tools.GetAssemblyProduct();

                //this.Context.CurrentProjectIsDirty = false;
                //this.Context.InvokeCurrentResourceBundleChanged();
                //this.RefreshButtonState();

                //// if there is only one resource set (the base one) the propose to create a new one
                //// to start translation
                //if (this.Context.CurrentResourceBundle.Count == 1)
                //{
                //    this.ResourceSet_Add();
                //}
            }
        }

        internal void RefreshCurrentBundle()
        {
            // load base file into grid
            DataGridViewRow newRow;
            //this.gridLoading = true;
            //try
            //{
            //    CustomDataGrid1.MainDataGridView.Rows.Clear();
            //    foreach (ResourceItem i in this.Context.CurrentBaseResourceSet.Values)
            //    {
            //        // show when
            //        //  - value is of type string
            //        var item = i as ResourceStringItem;
            //        if (item != null)
            //        {
            //            int newRowIndex = CustomDataGrid1.MainDataGridView.Rows.Add(null, item.Name, item.Comment, item.Value);
            //            newRow = CustomDataGrid1.MainDataGridView.Rows[newRowIndex];
            //            newRow.Tag = item;
            //            RowSetStatus(newRow, null);
            //            newRow.Cells[ColumnNames.LocalValue].ReadOnly = item.Locked;
            //            newRow.Cells[ColumnNames.LocalComment].ReadOnly = item.Locked;
            //            if (item.Locked)
            //            {
            //                newRow.Cells[ColumnNames.LocalValue].Style.BackColor = Color.AliceBlue;
            //                newRow.Cells[ColumnNames.LocalComment].Style.BackColor = Color.AliceBlue;
            //            }
            //        }
            //    }
            //}
            //finally
            //{
            //    this.gridLoading = false;
            //}

            //CustomDataGrid1.AutoSizeContent();

            //// load base file into tree view
            //string[] keySplit;
            //TreeView1.Nodes.Clear();
            //TreeView1.Nodes.Add("<root>", My.Resources.Local.TreeNavigatorRootText);
            //TreeView1.Nodes["<root>"].ImageKey = "resx";
            //TreeView1.Nodes["<root>"].SelectedImageKey = "resx";
            //TreeView1.Nodes.Add("<all>", My.Resources.Local.TreeNavigatorAllText);
            //TreeView1.Nodes["<all>"].ImageKey = "resx";
            //TreeView1.Nodes["<all>"].SelectedImageKey = "resx";

            //foreach (ResourceItem item in this.Context.CurrentBaseResourceSet.Values)
            //{
            //    if (item is ResourceStringItem)
            //    {
            //        // add value to tree
            //        keySplit = item.Name.Split(Properties.Settings.Default.PathSeparator.ToCharArray());
            //        TreeNode parentNode = TreeView1.Nodes["<root>"];
            //        for (int i = 0; i <= keySplit.Length - 2; i++)
            //        {
            //            if (parentNode.Nodes.ContainsKey(keySplit[i]))
            //            {
            //                parentNode = parentNode.Nodes[keySplit[i]];
            //            }
            //            else
            //            {
            //                parentNode = parentNode.Nodes.Add(keySplit[i], keySplit[i]);
            //                parentNode.ImageKey = "string";
            //                parentNode.SelectedImageKey = "string";
            //            }
            //        }
            //    }
            //}

            //TreeView1.Nodes["<root>"].Expand();
            //TreeView1.SelectedNode = TreeView1.Nodes["<root>"];

            //// load combo with cultures
            //CultureToolStripComboBox.Items.Clear();
            //foreach (ResourceSet i in this.Context.CurrentResourceBundle)
            //{
            //    CultureToolStripComboBox.Items.Add(i.Culture);
            //}

            //// make sure first visible row is active and selected
            //this.SelectFirstVisibleRow();

            //this.Context.CurrentLocalResourceSet = null;
            //if (CultureToolStripComboBox.Items.Count > 0)
            //{
            //    CultureToolStripComboBox.SelectedIndex = 0;
            //}
        }

        /// <summary>
        /// Makes sure first visible row is active and selected
        /// selected does not necesseraly means active
        /// (also, depending on the selected tree item above and current search criteria the first row may not be visible)
        /// </summary>
        private void SelectFirstVisibleRow()
        {
            //if (CustomDataGrid1.MainDataGridView.Rows.Count > 0)
            //{
            //    foreach (var row in this.CustomDataGrid1.MainDataGridView.Rows.OfType<DataGridViewRow>())
            //    {
            //        if (row.Visible)
            //        {
            //            // set selection if not already set to avoid redundant event triggering
            //            if (!row.Selected)
            //            {
            //                row.Selected = true;
            //            }

            //            // activate local value cell or base value cell if local one is not visible (no local resource set is active)
            //            var currentCell = row.Cells[ColumnNames.LocalValue].Visible ? row.Cells[ColumnNames.LocalValue] : row.Cells[ColumnNames.BaseValue];
            //            CustomDataGrid1.MainDataGridView.CurrentCell = currentCell;

            //            break;
            //        }
            //    }
            //}
        }

        private bool AskToSave()
        {
            //if (this.Context.CurrentProjectIsDirty)
            //{
            //    string message = String.Format(My.Resources.Local.SaveChangesWarning, this.Context.CurrentLocalResourceSet.Culture);
            //    switch (MessageBox.Show(message, Tools.GetAssemblyProduct(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
            //    {
            //        case DialogResult.Yes:
            //            this.ResourceBundle_Save();
            //            return true;
            //        case DialogResult.Cancel:
            //            return false;
            //        case DialogResult.No:
            //            return true;
            //        default:
            //            return false;
            //    }
            //}
            //else
            //{
            //    return true;
            //}
            return false;
        }

        private void ResourceBundle_Save()
        {
            //if (this.Context.CurrentProjectIsDirty)
            //{
            //    ResXResourceBundleAdapter resXResourceBundle = new ResXResourceBundleAdapter(new StandardAdapters.Common.FileSystem());
            //    resXResourceBundle.Save(this.Context.CurrentBaseFile, this.Context.CurrentResourceBundle);
            //    this.Context.CurrentProjectIsDirty = false;
            //}

            this.RefreshButtonState();
        }

        #endregion

        #region " Search and Filters "

        private void SearchToolStripTextBox_TextChanged(object sender, EventArgs e)
        {
            //SearchDelayTimer.Stop();
            //SearchDelayTimer.Start();
        }

        private void SearchDelayTimer_Tick(object sender, EventArgs e)
        {
            this.ApplyCriteria();
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.ApplyCriteria();
        }

        /// <summary>
        /// Apply selected criteria to grid. Criteria are the selected item in treeview and/or the search text
        /// </summary>
        private void ApplyCriteria()
        {
            //SearchDelayTimer.Stop();

            //// get search text
            //string searchText = SearchToolStripTextBox.Text.ToLower();

            //// get the selected path from the selected item of tree (eg. String1)
            //string treePath = "<all>";
            //if (TreeView1.SelectedNode != null)
            //{
            //    if (TreeView1.SelectedNode.Equals(TreeView1.Nodes["<all>"]))
            //    {
            //        treePath = "<all>";
            //    }
            //    else if (TreeView1.SelectedNode.Equals(TreeView1.Nodes["<root>"]))
            //    {
            //        treePath = "<root>";
            //    }
            //    else
            //    {
            //        treePath = TreeView1.SelectedNode.FullPath.Substring(TreeView1.Nodes["<root>"].Text.Length + 1);
            //    }
            //}

            //try
            //{
            //    CustomDataGrid1.SuspendLayout();

            //    string key;
            //    ResourceItem resourceItem;

            //    foreach (DataGridViewRow row in CustomDataGrid1.MainDataGridView.Rows)
            //    {
            //        resourceItem = (ResourceItem)row.Tag;

            //        // make row invisible. it will be decided later in this loop whether to show it
            //        row.Visible = false;

            //        // hide translated
            //        if (HideTranslatedToolStripMenuItem.Checked)
            //        {
            //            if (row.Cells[ColumnNames.LocalValue].Value != null && !String.IsNullOrEmpty(row.Cells[ColumnNames.LocalValue].Value.ToString()))
            //            {
            //                // leave row hidden and continue to next row
            //                continue;
            //            }
            //        }

            //        // hide locked
            //        if (HideLockedToolStripMenuItem.Checked)
            //        {
            //            // if current resource item is locked then consider in translated
            //            if (resourceItem.Locked && resourceItem.LockedReason == LockedReason.DeveloperLock)
            //            {
            //                // leave row hidden and continue to next row
            //                continue;
            //            }
            //        }

            //        // hide VS form items
            //        if (HideVSFormItemsToolStripMenuItem.Checked)
            //        {
            //            if (resourceItem.Locked && resourceItem.LockedReason == LockedReason.FrameworkLock)
            //            {
            //                continue;
            //            }
            //        }

            //        // hide items that are not translatable but just hold resex metadata
            //        if (resourceItem.Locked && resourceItem.LockedReason == LockedReason.ResexMetadata)
            //        {
            //            continue;
            //        }

            //        // check if row contains search text. if not row should not be visible.
            //        if (!String.IsNullOrEmpty(searchText))
            //        {
            //            bool searchTextOk = false;
            //            foreach (DataGridViewCell cell in row.Cells)
            //            {
            //                if (cell.OwningColumn.Visible)
            //                {
            //                    if (cell.Value != null && cell.Value.ToString().ToLower().IndexOf(searchText) != -1)
            //                    {
            //                        searchTextOk = true;
            //                        break;
            //                    }
            //                }
            //            }

            //            // if search text not found in row, leave it hidden and continue to next row
            //            if (!searchTextOk)
            //            {
            //                continue;
            //            }
            //        }

            //        // if '<all>' node is selected, show the row
            //        if (treePath == "<all>")
            //        {
            //            row.Visible = true;
            //            continue;
            //        }

            //        // get key of row (eg. String1_SubString1 which should be visible, or String_Placeholder1 which should be hidden)
            //        key = row.Cells[ColumnNames.Key].Value.ToString();

            //        // if no path is selected in tree
            //        if (treePath == "<root>")
            //        {
            //            // show row if key does not have separator (is a base string) and does not exist in tree as node (it will be shown when clicking on that node)
            //            if (key.IndexOf(Properties.Settings.Default.PathSeparator) == -1 && TreeView1.Nodes.Find(key, true).Length == 0)
            //            {
            //                row.Visible = true;
            //            }

            //            continue;
            //        }

            //        // if key or row is the same as selected tree node, show the row
            //        if (key == treePath)
            //        {
            //            row.Visible = true;
            //            continue;
            //        }

            //        // continue if current row (in this loop) starts with the selected path
            //        if (key.StartsWith(treePath))
            //        {
            //            // continue if current row (in this loop) is a direct child of current path and not the child of a child
            //            if (key.Substring(treePath.Length + 1).IndexOf(Properties.Settings.Default.PathSeparator) == -1 && key.Substring(treePath.Length, 1) == Properties.Settings.Default.PathSeparator)
            //            {
            //                row.Visible = true;
            //                continue;
            //            }
            //        }
            //    }
            //}
            //finally
            //{
            //    CustomDataGrid1.ResumeLayout();
            //}
        }

        #endregion

        #region " Form/Button State Refresh "

        private void CustomDataGrid1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (!this.formLoading && !this.gridLoading)
            //{
            //    this.Context.CurrentProjectIsDirty = true;
            //    this.RefreshButtonState();
            //}

            //// if there is no current resource set then do nothing - apparently the change was for other purpose
            //if (this.Context.CurrentLocalResourceSet == null)
            //{
            //    return;
            //}

            //this.UpdateResourceItemFromGridRow(CustomDataGrid1.GetRow(e.RowIndex));
        }

        private void RefreshButtonState()
        {
            //SaveToolStripMenuItem.Enabled = this.Context.CurrentProjectIsDirty;
            //AddCultureToolStripMenuItem.Enabled = this.Context.CurrentResourceBundle != null;
            //CopyBaseValueToolStripMenuItem.Enabled = this.Context.CurrentResourceBundle != null;
            //CopyBaseValueToolStripButton.Enabled = CopyBaseValueToolStripMenuItem.Enabled;
            //RemoveCultureToolStripMenuItem.Enabled = this.Context.CurrentLocalResourceSet != null;
            //CultureToolStripComboBox.Enabled = CultureToolStripComboBox.Items.Count > 0;
            //SearchToolStripTextBox.Enabled = this.Context.CurrentResourceBundle != null;
            //ShowWorkingFolderToolStripMenuItem.Enabled = this.Context.CurrentResourceBundle != null;
            //CustomDataGrid1.MainDataGridView.Columns[ColumnNames.BaseComment].Visible = BaseCommentToolStripMenuItem.Checked;
            //CustomDataGrid1.MainDataGridView.Columns[ColumnNames.LocalComment].Visible = LocalCommentToolStripMenuItem.Checked && this.Context.CurrentLocalResourceSet != null;
            //SplitContainer1.Panel1Collapsed = !TreeViewToolStripMenuItem.Checked;

            //CustomDataGrid1.MainDataGridView.Columns[ColumnNames.LocalValue].Visible = CultureToolStripComboBox.SelectedIndex != -1;

            //if (this.Context.CurrentLocalResourceSet == null)
            //{
            //    CustomDataGrid1.MainDataGridView.Columns[ColumnNames.LocalValue].HeaderText = My.Resources.Local.EditGridValueColumnHeader;
            //    CustomDataGrid1.MainDataGridView.Columns[ColumnNames.LocalComment].HeaderText = My.Resources.Local.EditGridCommentColumnHeader;
            //}
            //else
            //{
            //    CustomDataGrid1.MainDataGridView.Columns[ColumnNames.LocalValue].HeaderText = this.Context.CurrentLocalResourceSet.Culture + " " + My.Resources.Local.EditGridValueColumnHeader;
            //    CustomDataGrid1.MainDataGridView.Columns[ColumnNames.LocalComment].HeaderText = this.Context.CurrentLocalResourceSet.Culture + " " + My.Resources.Local.EditGridCommentColumnHeader;
            //}

            //SaveToolStripButton.Enabled = SaveToolStripMenuItem.Enabled;

            //this.RefreshStatusBar();

            //this.RefreshPlugInButtons();
        }

        private void RefreshStatusBar()
        {
            //// status bar text
            //string statusText = CustomDataGrid1.MainDataGridView.Rows.Count + " " + My.Resources.Local.Strings;
            //ItemsToolStripStatusLabel.Text = statusText;

            //// word count
            //string wordCountString = string.Empty;
            //if (this.Context.CurrentResourceBundle != null && this.Context.CurrentBaseResourceSet != null)
            //{
            //    wordCountString = My.Resources.Local.WordCountLabel + " : " + this.Context.CurrentBaseResourceSet.CountWords();
            //}

            //WordCountToolStripStatusLabel.Text = wordCountString;
        }

        private static void RowSetStatus(DataGridViewRow row, ResourceItem localResourceItem)
        {
            //ResourceItem baseResourceItem = (ResourceItem)row.Tag;

            //// set color (mark translated with gray color)
            //if (localResourceItem != null && !localResourceItem.ValueEmpty)
            //{
            //    row.Cells[ColumnNames.Key].Style.ForeColor = Color.DarkGray;
            //}
            //else
            //{
            //    row.Cells[ColumnNames.Key].Style.ForeColor = SystemColors.ControlText;
            //}

            //row.Cells[ColumnNames.BaseValue].Style.ForeColor = row.Cells[ColumnNames.Key].Style.ForeColor;
            //row.Cells[ColumnNames.BaseComment].Style.ForeColor = row.Cells[ColumnNames.Key].Style.ForeColor;
            //row.Cells[ColumnNames.LocalValue].Style.BackColor = Color.Empty;        // reset to empty - used when marking for reviewing
            //row.Cells[ColumnNames.LocalComment].Style.BackColor = Color.Empty;      // reset to empty - used when marking for reviewing

            //// set icon
            //if (baseResourceItem is ResourceStringItem)
            //{
            //    if (baseResourceItem.Locked)
            //    {
            //        row.Cells[ColumnNames.Icon].Value = My.Resources.Local.resString_locked;
            //    }
            //    else
            //    {
            //        if (localResourceItem != null && localResourceItem.ReviewPending)
            //        {
            //            row.Cells[ColumnNames.Icon].Value = My.Resources.Local.resString_review;
            //            row.Cells[ColumnNames.LocalValue].Style.BackColor = Color.Yellow;
            //            row.Cells[ColumnNames.LocalComment].Style.BackColor = Color.Yellow;
            //        }
            //        else if (localResourceItem != null && !localResourceItem.ValueEmpty)
            //        {
            //            row.Cells[ColumnNames.Icon].Value = My.Resources.Local.resString_translated;
            //        }
            //        else
            //        {
            //            row.Cells[ColumnNames.Icon].Value = My.Resources.Local.resString;
            //        }
            //    }
            //}
            //else
            //{
            //    row.Cells[ColumnNames.Icon].Value = null;
            //}
        }

        #endregion

        #region " Settings Save and Restore "

        private void Settings_Restore()
        {
            //// recent items
            //RecentItemsManager.Fill(Properties.Settings.Default.RecentFiles, Properties.Settings.Default.RecentFilesCount);

            //BaseCommentToolStripMenuItem.Checked = Properties.Settings.Default.BaseCommentColumnVisible;
            //LocalCommentToolStripMenuItem.Checked = Properties.Settings.Default.LocalCommentColumnVisible;
            //TreeViewToolStripMenuItem.Checked = Properties.Settings.Default.TreeViewVisible;

            //if (Properties.Settings.Default.FontSize > 0)
            //{
            //    CustomDataGrid1.SetGridFont(Properties.Settings.Default.FontSize);
            //}

            //HideLockedToolStripMenuItem.Checked = Properties.Settings.Default.HideLocked;
            //HideVSFormItemsToolStripMenuItem.Checked = Properties.Settings.Default.HideVSFormItems;
        }

        private void Settings_Save()
        {
            //// recent items
            //Properties.Settings.Default.RecentFiles = RecentItemsManager.ConvertToXml();

            //Properties.Settings.Default.BaseCommentColumnVisible = BaseCommentToolStripMenuItem.Checked;
            //Properties.Settings.Default.LocalCommentColumnVisible = LocalCommentToolStripMenuItem.Checked;
            //Properties.Settings.Default.TreeViewVisible = TreeViewToolStripMenuItem.Checked;
            //Properties.Settings.Default.TreeViewVisible = TreeViewToolStripMenuItem.Checked;
            //Properties.Settings.Default.FontSize = CustomDataGrid1.MainDataGridView.Font.Size;
            //Properties.Settings.Default.HideLocked = HideLockedToolStripMenuItem.Checked;
            //Properties.Settings.Default.HideVSFormItems = HideVSFormItemsToolStripMenuItem.Checked;
            //Properties.Settings.Default.Save();
        }

        #endregion
    }
}
